include ../config.mk

SRCS =  \
	Class.m \
	GetNewHaskellData.m \
	MemoryManagement.m \
	ObjectMap.m \
	Selector.m \
	FFICallInterface.m \
	Invocation.m \
	NewClass.m \
	RetainedHaskellPart.m \
	Marshalling.m \
    $(FFI_SOURCES) \
	Exceptions.m \
	Statistics.m \
	$(NULL)

dist_srcdir = HOC_cbits
dist_FILES = \
	$(SRCS) \
	Makefile.in \
	$(wildcard *.h) \
	$(NULL)

OBJS_tmp1 = $(SRCS:%.c=%.o)	
OBJS_tmp2 = $(OBJS_tmp1:%.S=%.o)	
OBJS = $(OBJS_tmp2:%.m=%.o)

CFLAGS += -isysroot /Developer/SDKs/MacOSX10.4u.sdk -g $(FFI_CFLAGS) -fno-common -DMACOSX
ASFLAGS += $(FFI_CFLAGS) -DMACOSX

ifeq "$(HocBuildDylibs)" "YES"
LIBRARIES=libHOC_cbits.a libHOC_cbits.dylib
else
LIBRARIES=libHOC_cbits.a HOC_cbits.o
endif

HOCLIBDIR="$(destdir)"/$(GHC_LIB_PATH)/HOC

all: $(LIBRARIES)

clean:
	rm -rf libHOC_cbits.a HOC_cbits.o $(OBJS) depend

HOC_cbits.o: libHOC_cbits.a
	ld -r -x -o HOC_cbits.o $(FFI_LIBS) $(ALL_LOAD) libHOC_cbits.a
	if test $(PLATFORM) == gnustep; then \
	  strip --strip-debug HOC_cbits.o; \
	fi

libHOC_cbits.a: $(OBJS)
	$(MAKE_STATIC_LIB) libHOC_cbits.a $(OBJS)

libHOC_cbits.dylib: libHOC_cbits.a
	export MACOSX_DEPLOYMENT_TARGET=10.3 && \
	 gcc \
	 -fPIC \
	 $(FFI_LIBS) \
	 -all_load \
	 -dynamiclib \
	 -undefined dynamic_lookup \
	 -lobjc \
	 -framework Foundation \
	 -single_module \
	 -o $@ \
	 $<
	install_name_tool -id "`pwd`/$@" $@

%.o: %.m
	gcc -c $(CFLAGS) -fPIC -o $@ $<
		
depend: $(SRCS)
	cc -MM $(CFLAGS) $(SRCS) > depend
		
install: install-files
	ranlib $(HOCLIBDIR)/libHOC_cbits.a
ifeq "$(HocBuildDylibs)" "YES"
	install_name_tool -id $(HOCLIBDIR)/libHOC_cbits.dylib $(HOCLIBDIR)/libHOC_cbits.dylib
endif

install-files: all
	mkdir -p $(HOCLIBDIR)
	cp -R $(LIBRARIES) $(HOCLIBDIR)
	
-include depend

